Action = Action or Class("Action")

---@diagnostic disable-next-line: duplicate-set-field
function Action:ctor(duration, props, opts)
    self.duration = duration
    self.props = props or {}
    self.opts = opts
    self.relative = false
    self.easing = opts and opts.easing or EasingMethods.linear
    self.countTime = 0
    self.completed =  false
end

function Action:set(time)
    if self.completed then
        return
    end
    self.countTime = time
    local progress = self.duration == 0 and 1 or self.countTime / self.duration
    progress = math.min(progress, 1)
    progress = self.easing(progress)
    for k, v in pairs(self.props) do
        self.target[k] = self.startProps[k] + (v - self.startProps[k]) * progress
    end
    if progress == 1 then
        self.completed = true
        if self.opts and self.opts.callback then
            self.opts.callback()
        end
    end
end
function Action:update(dt)
    self.countTime = self.countTime + dt
    self:set(self.countTime)
end

function Action:start(target)
    self.target = target
    self.startProps = {}
    for k, _ in pairs(self.props) do
        self.startProps[k] = self.target[k]
    end
end

ActionSequeue = ActionSequeue or Class("ActionSequeue")
---@diagnostic disable-next-line: duplicate-set-field
function ActionSequeue:ctor(actions)
    self.actions = actions
end

function ActionSequeue:start(target)
    self.countTime = 0
end

-- TODO tween动作的结束和删除等管理
function ActionSequeue:update(dt)
    self.countTime = self.countTime + dt
    local startTime, endTime = 0, 0
    local ended = true
    for _, action in ipairs(self.actions) do
        endTime = endTime + action.duration
        action:set(self.countTime - startTime)
        if self.countTime < endTime then
            ended = false
            break;
        end
        startTime = startTime + action.duration
    end
    return ended
end
